﻿using System;
using System.Collections.Generic;

namespace Synergy.SharePoint.Configuration
{
	/// <summary>
	/// Represents a configuration section metadata definition.
	/// </summary>
	public sealed class ConfigurationSectionGroup : ConfigurationSectionBase
	{
		#region | Fields |

		private readonly List<ConfigurationSectionGroup> _sectionGroups = new List<ConfigurationSectionGroup>();
		private readonly List<ConfigurationSection> _sections = new List<ConfigurationSection>();

		#endregion


		#region | Initialization |

		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigurationSectionGroup"/> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="type">The type.</param>
		public ConfigurationSectionGroup(string name, Type type) : base(name, type) { }


		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigurationSectionGroup"/> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="type">The type.</param>
		public ConfigurationSectionGroup(string name, string type) : base(name, type) { }


		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigurationSectionGroup"/> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="type">The type.</param>
		/// <param name="children">The children.</param>
		public ConfigurationSectionGroup(string name, Type type, params ConfigurationSectionBase[] children) : base(name, type)
		{
			AddChildren(children);
		}


		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigurationSectionGroup"/> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="type">The type.</param>
		/// <param name="children">The children.</param>
		public ConfigurationSectionGroup(string name, string type, params ConfigurationSectionBase[] children) : base(name, type)
		{
			AddChildren(children);
		}

		#endregion


		#region | Properties |

		/// <summary>
		/// Gets the section groups.
		/// </summary>
		/// <value>The section groups.</value>
		public List<ConfigurationSectionGroup> SectionGroups
		{
			get { return this._sectionGroups; }
		}


		/// <summary>
		/// Gets the sections.
		/// </summary>
		/// <value>The sections.</value>
		public List<ConfigurationSection> Sections
		{
			get { return this._sections; }
		}

		#endregion


		#region | Methods |

		/// <summary>
		/// Adds the children.
		/// </summary>
		/// <param name="children">The children.</param>
		private void AddChildren(IList<ConfigurationSectionBase> children)
		{
			if (children == null)
			{
				return;
			}
			for (var i = 0; i < children.Count; i++)
			{
				var group = children[i] as ConfigurationSectionGroup;
				if (group != null)
				{
					SectionGroups.Add(group);
				}
				var section = children[i] as ConfigurationSection;
				if (section != null)
				{
					Sections.Add(section);
				}
			}
		}

		#endregion
	}
}